﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels.Projiect_LX;
using VOL.System.IRepositories.System.Project;
using VOL.System.IServices.Project_LX;

namespace VOL.System.Services.Projecct_LX.partial
{
    /// <summary>
    /// 项目来源服务
    /// </summary>
    public partial class ProSourceInfoService
    {
        private IProjectsourceInfoRepository _repository;

        [ActivatorUtilitiesConstructor]
        public ProSourceInfoService(IProjectsourceInfoRepository repository)
        {
            _repository = repository;
        }
        WebResponseContent webResponse=new WebResponseContent();

        /// <summary>
        /// 获取项目来源信息
        /// </summary>
        /// <returns></returns>
        public async Task<WebResponseContent> GetSourceListAsync()
        {
            var data = await GetChildrenListAsync("XMLY");
            return webResponse.OK(null, data);
        }

        private async Task<List<object>> GetChildrenListAsync(string dicCode)
        {
            //获取所有一级信息
            var SourceFirstList= await _repository.FindAsIQueryable(x=>x.dicCode == dicCode).ToListAsync();
            //实例化匿名类集合
            List<object> childrenList = new List<object>();
            //遍历一级信息
            foreach (var item in SourceFirstList)
            {
                //查看是否有子集
                var line = _repository.FindAsIQueryable(x => x.dicCode.Equals(item.dicCode)).Count();
                if(line>0)
                {
                    //给匿名类赋值
                    childrenList.Add(new
                    {
                        item.Id,
                        item.dicValue,
                        item.dicKey,
                        item.dicCode,
                        item.orgId,
                        item.orgName,
                        item.productId,
                        item.productName,
                        children= GetChildrenListAsync(item.dicCode)
                    });
                }
                else
                {
                    childrenList.Add(new
                    {
                        item.Id,
                        item.dicValue,
                        item.dicKey,
                        item.dicCode,
                        item.orgId,
                        item.orgName,
                        item.productId,
                        item.productName,                       
                    });
                }
            }

            return  childrenList;
        }
    }
}
